package org.dromara.agriculture.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.agriculture.domain.bo.AgricultureLandBo;
import org.dromara.agriculture.domain.vo.AgricultureLandVo;
import org.dromara.agriculture.domain.AgricultureLand;
import org.dromara.agriculture.mapper.AgricultureLandMapper;
import org.dromara.agriculture.service.IAgricultureLandService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 地块Service业务层处理
 *
 * @author Robin
 * @date 2024-12-05
 */
@RequiredArgsConstructor
@Service
public class AgricultureLandServiceImpl implements IAgricultureLandService {

    private final AgricultureLandMapper baseMapper;

    /**
     * 查询地块
     *
     * @param landId 主键
     * @return 地块
     */
    @Override
    public AgricultureLandVo queryById(Long landId){
        return baseMapper.selectVoById(landId);
    }

    /**
     * 分页查询地块列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 地块分页列表
     */
    @Override
    public TableDataInfo<AgricultureLandVo> queryPageList(AgricultureLandBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AgricultureLand> lqw = buildQueryWrapper(bo);
        Page<AgricultureLandVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的地块列表
     *
     * @param bo 查询条件
     * @return 地块列表
     */
    @Override
    public List<AgricultureLandVo> queryList(AgricultureLandBo bo) {
        LambdaQueryWrapper<AgricultureLand> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AgricultureLand> buildQueryWrapper(AgricultureLandBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AgricultureLand> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getLandName()), AgricultureLand::getLandName, bo.getLandName());
        lqw.eq(StringUtils.isNotBlank(bo.getLandType()), AgricultureLand::getLandType, bo.getLandType());
        lqw.eq(bo.getLandArea() != null, AgricultureLand::getLandArea, bo.getLandArea());
        lqw.eq(bo.getStrokeWeight() != null, AgricultureLand::getStrokeWeight, bo.getStrokeWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getStrokeColor()), AgricultureLand::getStrokeColor, bo.getStrokeColor());
        lqw.eq(bo.getStrokeOpacity() != null, AgricultureLand::getStrokeOpacity, bo.getStrokeOpacity());
        lqw.eq(StringUtils.isNotBlank(bo.getLandPath()), AgricultureLand::getLandPath, bo.getLandPath());
        lqw.eq(StringUtils.isNotBlank(bo.getFillColor()), AgricultureLand::getFillColor, bo.getFillColor());
        lqw.eq(bo.getFillOpacity() != null, AgricultureLand::getFillOpacity, bo.getFillOpacity());
        lqw.eq(bo.getCurrentBatch() != null, AgricultureLand::getCurrentBatch, bo.getCurrentBatch());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AgricultureLand::getStatus, bo.getStatus());
        lqw.eq(bo.getOrderNum() != null, AgricultureLand::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getBaseId() != null, AgricultureLand::getBaseId, bo.getBaseId());
        lqw.eq(bo.getUserId() != null, AgricultureLand::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增地块
     *
     * @param bo 地块
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AgricultureLandBo bo) {
        AgricultureLand add = MapstructUtils.convert(bo, AgricultureLand.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLandId(add.getLandId());
        }
        return flag;
    }

    /**
     * 修改地块
     *
     * @param bo 地块
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AgricultureLandBo bo) {
        AgricultureLand update = MapstructUtils.convert(bo, AgricultureLand.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AgricultureLand entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除地块信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
